package com.ys.code.generator;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import static com.ys.code.generator.common.Constant.controllerPackageName;
import static com.ys.code.generator.common.Constant.daoPackageName;
import static com.ys.code.generator.common.Constant.domainName;
import static com.ys.code.generator.common.Constant.packageName;
import static com.ys.code.generator.common.Constant.poPackageName;
import static com.ys.code.generator.common.Constant.servicePackageName;
import static com.ys.code.generator.common.Constant.sourcePath;
import static com.ys.code.generator.common.Constant.targetPath;
import static com.ys.code.generator.common.Constant.voPackageName;

/**
 * <p>@description:生成通用的controller,Service,Dao层数据</p>
 *
 * @author shuang.yang
 * @@version 1.0:
 * @date 2019-07-31
 */
public class Main {


    public static void main(String[] args) throws Exception {

        Map<String, Object> map = new HashMap();
        //模板
        map.put("DaoTemplate.java.vm", "dao/" + domainName + "Dao.java");
        map.put("ServiceTemplate.java.vm", "service/" + domainName + "Service.java");
        map.put("ServiceImplTemplate.java.vm", "service/impl/" + domainName + "ServiceImpl.java");
        map.put("ControllerTemplate.java.vm", "controller/" + domainName + "Controller.java");
        map.put("MapperxmlTemplate.xml.vm", "dto/" + domainName + "Mapper.xml");
        for (String templateFile : map.keySet()) {
            String targetFile = (String) map.get(templateFile);
            Properties pro = new Properties();
            pro.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
            pro.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
            pro.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, sourcePath);
            VelocityEngine ve = new VelocityEngine(pro);

            VelocityContext context = new VelocityContext();

            //统一名称
            context.put("domainName", domainName);
            context.put("packageName", packageName);
            context.put("controllerPackageName", controllerPackageName);
            context.put("servicePackageName", servicePackageName);
            context.put("daoPackageName", daoPackageName);
            context.put("poPackageName", poPackageName);
            context.put("voPackageName", voPackageName);

            Template t = ve.getTemplate(templateFile, "UTF-8");

            File file = new File(targetPath, targetFile);
            if (!file.getParentFile().exists())
                file.getParentFile().mkdirs();
            if (!file.exists())
                file.createNewFile();

            FileOutputStream outStream = new FileOutputStream(file);
            OutputStreamWriter writer = new OutputStreamWriter(outStream,
                    "UTF-8");
            BufferedWriter sw = new BufferedWriter(writer);
            t.merge(context, sw);
            sw.flush();
            sw.close();
            outStream.close();
            System.out.println("成功生成Java文件:"
                    + (targetPath + targetFile).replaceAll("/", "\\\\"));
        }
    }
}
